package com.note.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;

@Configuration
public class JedisPoolConfig {
    @Value("${spring.data.redis.database}")
    private Integer database;

    @Value("${spring.data.redis.host}")
    private String host;

    @Value("${spring.data.redis.port}")
    private int port;

    @Value("${spring.data.redis.password}")
    private String password;

    @Value("${spring.data.redis.timeout}")
    private int timeout;

    @Value("${spring.data.redis.jedis.pool.max-active}")
    private int maxActive;

    @Value("${spring.data.redis.jedis.pool.max-idle}")
    private int maxIdle;

    @Value("${spring.data.redis.jedis.pool.min-idle}")
    private int minIdle;

    @Value("${spring.data.redis.jedis.pool.max-wait}")
    private long maxWaitMillis;

    /**
     * 初始化连接池
     */
    @Bean
    @SuppressWarnings("deprecation")
    public JedisPool generateJedisPool(){
        redis.clients.jedis.JedisPoolConfig jedisPoolConfig =  new redis.clients.jedis.JedisPoolConfig();
        //取消MXBean注册，否则会出现重复建立而失败
        jedisPoolConfig.setJmxEnabled(false);
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        jedisPoolConfig.setMaxTotal(maxActive);
        jedisPoolConfig.setMinIdle(minIdle);
        //链接耗尽时是否阻塞，false报异常，true阻塞直到超时，默认true
        jedisPoolConfig.setBlockWhenExhausted(Boolean.TRUE);
        return new JedisPool(jedisPoolConfig,host,port,timeout,password,database);
    }
}
